﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Linkmeme.Models;

namespace Linkmeme
{
    class ViewDataBuilder<TSource> where TSource : SourceBase
    {
        public static SourceViewData<TSource> BuildForViewAll(Guid currentUserId, string groupBy)
        {
            //SourceProviderFactory<TSource> factory = new SourceProviderFactory<TSource>();
            LinkmemeDataContext context = new LinkmemeDataContext();
            List<UserSelectedProvider> userProvList = context.GetProvidersByType(currentUserId, GetTypeName(typeof(TSource)));
            SourceViewData<TSource> itemList = new SourceViewData<TSource>();
            foreach (UserSelectedProvider p in userProvList)
            {
                itemList.Items.AddRange(GetUserFeed(p.ProviderID, p.ProviderUserName));
            }

            itemList.GroupType = groupBy; //groupBy.HasValue ? groupBy.Value : "Provider";

            return itemList;
        }

        public static SourceViewData<TSource> BuildForView(int providerId, string providerUserName)
        {
            SourceViewData<TSource> itemList = new SourceViewData<TSource>();
            itemList.Items = GetUserFeed(providerId, providerUserName); 

            return itemList;
        }


        private static string GetTypeName(Type t)
        {
            string ts = t.ToString().ToLower();
            if (ts.EndsWith("picture"))
                return "photo";
            return ts.Substring(ts.LastIndexOf(".") + 1);
        }
        

        // add cache ability
        private static List<TSource> GetUserFeed(int providerId, string providerUserName)
        {
            string cacheKey = BuildCacheKey(providerId, providerUserName);

            List<TSource> imgList = HttpContext.Current.Cache[cacheKey] as List<TSource>;
            if (imgList == null)
            {
                
                SourceProviderFactory<TSource> factory = new SourceProviderFactory<TSource>();
                ISourceProvider<TSource> provider = factory.CreateRssSourceProvider(providerId);
                imgList = provider.GetUserFeed(providerUserName);

                
                HttpContext.Current.Cache.Add(
                       cacheKey,
                       imgList,
                       null,
                       System.Web.Caching.Cache.NoAbsoluteExpiration,
                       new TimeSpan(0, 0, 180),
                       System.Web.Caching.CacheItemPriority.Normal,
                       null);
            }
            return imgList;
        }

        private static string BuildCacheKey(int providerId, string username)
        {
            return string.Format("{0}-{1}", providerId, username);
        }
    }
}

